//
//  CommonWebVC.m
//  XV
//
//  Created by apple on 03/04/2019.
//  Copyright © 2019 XR. All rights reserved.
//

#import "CommonWebVC.h"


@interface CommonWebVC ()<WKUIDelegate,WKNavigationDelegate>

@end


@implementation CommonWebVC


//初始化界面
- (instancetype)initWithURLString:(NSString*)url title:(NSString*)title
{
    self = [super init];
    if (self) {
        if ( [url hasPrefix:@"http://"]==NO
            && [url hasPrefix:@"https://"]==NO
            && [url hasPrefix:@"www."]==YES)
        {
            url = [@"http://" stringByAppendingString:url];
        }
        _webUrl = [NSURL URLWithString:url];
        self.title = title;
    }
    return self;
}


- (instancetype)initWithURL:(NSURL*)url title:(NSString*)title
{
    self = [super init];
    if (self) {
        _webUrl = url;
        self.title = title;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    CGRect rc = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
    configuration.allowsInlineMediaPlayback = YES;
    _webView = [[WKWebView alloc] initWithFrame:rc configuration:configuration];
    _webView.UIDelegate=self;
    _webView.navigationDelegate=self;
    [self.view addSubview:_webView];
    
    if (self.isHideTitle)
    {
        [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(LL_StatusBarHeight);
            make.left.right.bottom.equalTo(self.view);
        }];
    }
    else
    {
        [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.view);
        }];
    }
    
    [self setup];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    if (self.isHideTitle)
    {
        self.navigationController.navigationBar.hidden = YES;
    }
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    
    if (self.isHideTitle)
    {
        self.navigationController.navigationBar.hidden = NO;
    }
}


- (void)setup
{
    if (_webUrl!=nil)
    {
        [self reloadUrl];
    }
    
}

//重新加载url
-(void)reloadUrl
{
    NSURLRequest* requeest = [[NSURLRequest alloc]initWithURL:_webUrl];
    [_webView loadRequest:requeest];
}

#pragma mark - action
- (void)LeftButtonDidClick:(UIButton *)button
{
    if (self.isHideTitle)
    {
        self.navigationController.navigationBar.hidden = NO;
    }
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
    
    if (kDictIsEmpty(self.dicHttpHeader))
    {
        decisionHandler(WKNavigationActionPolicyAllow);//允许跳转
        return;
    }
    
    NSMutableURLRequest *mutableRequest = [navigationAction.request mutableCopy];
    NSDictionary *requestHeaders = navigationAction.request.allHTTPHeaderFields;
    if (requestHeaders[@"authorization"])
    {
        decisionHandler(WKNavigationActionPolicyAllow);//允许跳转
    }
    else
    {
        //这里添加请求头，把需要的都添加进来
        for (NSString* key in self.dicHttpHeader)
        {
            NSString* value = [self.dicHttpHeader objectForKey:key];
            [mutableRequest setValue:value forHTTPHeaderField:key];
        }
        
        [webView loadRequest:mutableRequest];
        decisionHandler(WKNavigationActionPolicyAllow);//允许跳转
    }
    
}

@end
